package com.colortea.action.frontstage.pay;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.colortea.action.backstage.base.BaseTAction;
import com.colortea.action.backstage.utils.BaseTools;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.action.frontstage.alipay.util.AlipayNotify;
import com.colortea.entity.MemberT;
import com.colortea.entity.OrderT;
import com.colortea.service.MemberTService;
import com.colortea.service.OrderTService;

@ParentPackage("colortea")
@Namespace("/pay")
@InterceptorRefs({ @InterceptorRef("defaultStack") })
public class AliPayAction extends BaseTAction {

	@Resource
	private OrderTService orderTService;
	@Resource
	private MemberTService memberTService;

	/**
	 * 支付宝商户订单号 由商户自己生成
	 */
	private String out_trade_no;
	/**
	 * 支付宝交易号 支付宝异步提供
	 */
	private String trade_no;
	/**
	 * 支付宝交易状态 支付宝异步提供
	 */
	private String trade_status;

	private HttpServletRequest request;
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	public String getTrade_no() {
		return trade_no;
	}

	public void setTrade_no(String trade_no) {
		this.trade_no = trade_no;
	}

	public String getTrade_status() {
		return trade_status;
	}

	public void setTrade_status(String trade_status) {
		this.trade_status = trade_status;
	}
	
	@Action(value = "updateOrderState")
	public void updateOrderState() {
		Map<String, String> params = new HashMap<String, String>();
//		HttpServletRequest request = ServletActionContext.getRequest();
		Map requestParams = ServletActionContext.getRequest().getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
			params.put(name, valueStr);
		}
		System.out.println("---------------------------------------------------------------------------");
		System.out.println("Out_trade_no-----"+StringUtils.isNotBlank(this.getOut_trade_no())+"       "+ this.getOut_trade_no());
		System.out.println("Trade_no-----"+StringUtils.isNotBlank(this.getTrade_no())+"          "+this.getTrade_no());
		System.out.println("Trade_status-----"+StringUtils.isNotBlank(this.getTrade_status())+"          "+this.getTrade_status());
		System.out.println("params-----------" + AlipayNotify.verify(params));
		System.out.println("---------------------------------------------------------------------------");
		if (StringUtils.isNotBlank(this.getOut_trade_no()) && StringUtils.isNotBlank(this.getTrade_no()) && StringUtils.isNotBlank(this.getTrade_status())) {
			if (AlipayNotify.verify(params)) {
				if (this.getTrade_status().equals("TRADE_FINISHED")) {
					updateOrder();
				} else if (this.getTrade_status().equals("TRADE_SUCCESS")) {
					updateOrder();
				}
			}
		}
	}

	public void updateOrder() {
		OrderT order = this.orderTService.findByPK(OrderT.class, this.getOut_trade_no());
		if (order != null) {
			System.out.println("-----------------------");
			order.setOrderstate(StaticKey.ORDERSTATE_THREE_NUM);// 待确认收获
			order.setPaystate(StaticKey.PAYSTATE_ONE_NUM);// 已付款
			order.setShippingstate(StaticKey.SHIPPINGSTATE_NOT_DELIVER_ZERO_NUM);// 配货中
			order.setPaytime(BaseTools.getSystemTime());
			order.setPurchasetime(BaseTools.getSystemTime());
			orderTService.update(order);
			MemberT member = this.memberTService.findByPK(MemberT.class, order.getMemberid());
			member.setIntegration((int) order.getShouldpay());
			memberTService.update(member);
		}
	}

}
